package com.googlecode.funclate;

import com.googlecode.totallylazy.Option;

import java.util.List;
import java.util.Map;

public abstract class AbstractModel implements Model {
    @Override
    public Model addOptionally(final String key, final Object value) {
        if (value instanceof Option) {
            return addOptionallyOption(key, (Option<?>) value);
        }
        if (value instanceof List) {
            return addOptionallyList(key, (List<?>) value);
        }
        if (value instanceof Map) {
            return addOptionallyMap(key, (Map<?, ?>) value);
        }
        return add(key, value);
    }

    private Model addOptionallyOption(String key, Option<?> option) {
        if (option.isEmpty()) return this;
        return add(key, option.value());
    }

    private Model addOptionallyList(String key, List<?> list) {
        if (list.isEmpty()) return this;
        return add(key, list);
    }

    private Model addOptionallyMap(String key, Map<?, ?> map) {
        if (map.isEmpty()) return this;
        return add(key, map);
    }
}
